package com.sm;

import gynsh.scf.basics.channelauth.ChannelAuthService;
import gynsh.scf.basics.config.filter.HttpServletRequestWrapFilter;
import gynsh.scf.basics.define.vo.ResultVo;
import gynsh.scf.basics.dict.ChannelType;
import gynsh.scf.basics.encrypt.EncryptService;
import gynsh.scf.basics.encrypt.bean.EncryptBean;
import gynsh.scf.basics.encrypt.sm.thirdSM.ThirdSignVerifyService;
import gynsh.scf.basics.utils.RequestUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;

@Service
@Slf4j
public class SignService {
    @Autowired
    private ThirdSignVerifyService thirdSignVerifyService;

    @Autowired
    private EncryptService encryptService;
    /**
     * 返回值数据签名
     */
    public void sign(ResultVo resultVo){
        HttpServletRequest request = RequestUtils.getRequest();
        if(encryptService.isIgnoreUrl(request)){
            return;
        }
        Integer channelCode = ChannelAuthService.getCurChannel();

        if(ChannelType.CLR3_WEB.getCode().equals(channelCode)){
            // 超链融不需要签名
            return;
        }else{
            thirdSignVerifyService.signForChannel(channelCode,resultVo);
        }

    }

    /**
     * 验签
     * @param requestWrapper
     */
    public void verify(HttpServletRequestWrapFilter.RequestBodyServletRequestWrapper requestWrapper){
        if(encryptService.isIgnoreUrl(requestWrapper)){
            return;
        }

        EncryptBean requestParam = EncryptService.getRawParam(requestWrapper);

        Integer channelCode = ChannelAuthService.getCurChannel();
        if(ChannelType.CLR3_WEB.getCode().equals(channelCode)){
            // 超链融不需要验签
            return;
        }else{
            thirdSignVerifyService.verifyForChannel(ChannelAuthService.getCurChannel(),
                    String.valueOf(requestParam.getParam()),requestParam.getSign());
        }
    }
}
